package com.share.finance.risk.rules.web;

import com.share.common.common.Page;
import com.share.common.common.Result;
import com.share.finance.risk.rules.model.db.Project;
import com.share.finance.risk.rules.model.vo.ProjectAddOrUpdateReq;
import com.share.finance.risk.rules.model.vo.ProjectPageReq;
import com.share.finance.risk.rules.model.vo.VariateVo;
import com.share.finance.risk.rules.service.IProjectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 工程表  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-11-24
 */
@RequestMapping(value = "/project")
@RestController
public class ProjectController {

    @Autowired
    private IProjectService iProjectService;

    @ApiOperation(value = "激活工程版本")
    @RequestMapping(value = "/activateVersion")
    public Result activateProjectVersion(String version, Long projectId) {
        if (StringUtils.isEmpty(version) || projectId == null) {
            return Result.failure("version或者projectId不能为空");
        }
        Integer status = iProjectService.activateProjectVersion(projectId, version);
        return Result.insertOrDeleteOrUpdate(status);
    }

    @ApiOperation(value = "获取某个工程的所有变量")
    @RequestMapping(value = "/listVariate")
    public Result listVariate(String productCode, String eventType) {
        if (StringUtils.isEmpty(productCode) || StringUtils.isEmpty(eventType)) {
            return Result.failure("productCode或者eventType不能为空");
        }
        List<VariateVo> list = iProjectService.listByProductCodeAndEventType(productCode, eventType);
        return Result.success(list);
    }

    @ApiOperation(value = "获取所有工程")
    @RequestMapping(value = "/pageList")
    public Result<Page<Project>> pageList(ProjectPageReq req) {
        Page<Project> page = iProjectService.pageList(req);
        return Result.success(page);
    }

    @ApiOperation("新增")
    @RequestMapping("/add")
    public Result add(ProjectAddOrUpdateReq req) {
        Project record = new Project();
        BeanUtils.copyProperties(req, record);
        return Result.bool(iProjectService.save(record));
    }

    @ApiOperation("修改")
    @RequestMapping("/update")
    public Result update(ProjectAddOrUpdateReq req) {
        if (req.getId() == null) {
            return Result.failure("参数异常!");
        }
        Project record = new Project();
        BeanUtils.copyProperties(req, record);
        return Result.bool(iProjectService.updateById(record));
    }

    @ApiOperation("删除")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        return Result.bool(iProjectService.removeById(id));
    }
}
